<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ title }}</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="案例分类" prop="caseClassId">
          <treeselect
            v-model="form.caseClassId"
            :disable-branch-nodes="true"
            :options="caseClassList"
            :normalizer="normalizer"
            placeholder="请选择案例分类"
          />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" clearable/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者" clearable/>
        </el-form-item>
        <el-form-item label="摘要" prop="excerpt">
          <el-input v-model="form.excerpt" placeholder="请输入摘要" clearable/>
        </el-form-item>
        <el-form-item label="案例配图" prop="picture">
          <image-upload v-model="form.picture" :limit="1" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>预览</span>
      </div>
      <div class="preview">
        <div class="preview-title">{{ form.title || "请输入标题" }}</div>
        <div class="preview-info">
          <div class="info-text">时间：2022-09-22</div>
          <div class="info-text">阅读量：22</div>
          <div class="info-text">来源: {{ form.author || "南昌众康" }}</div>
        </div>
        <div class="preview-excerpt" v-if="form.excerpt">
          {{ form.excerpt }}
        </div>
        <div class="preview-content">
          <div v-html="form.content" class="ql-editor"></div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  listCase,
  getCase,
  delCase,
  addCase,
  updateCase,
} from "@/api/chronicDisease/case";
import { listCaseClass } from "@/api/chronicDisease/caseClass";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Case",
  dicts: ["yy_content_status"],
  components: {
    Treeselect,
  },
  data() {
    return {
      title: "新增案例", //
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        caseClassId: [
          { required: true, message: "请选择文章分类", trigger: "change" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        excerpt: [{ required: true, message: "摘要不能为空", trigger: "blur" }],
        picture: [{ required: true, message: "请上传案例配图", trigger: "blur" }],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
      },
      id: "",
      caseClassList: [],
    };
  },
  created() {
    if (this.$route.query.id) {
      // 有id就修改
      this.id = this.$route.query.id;
      this.getCaseInfo(this.$route.query.id); //获取表单数据
    } else {
      this.reset();
    }
    this.title = this.$route.query.metaTitle;
    this.getCaseClassList();
  },
  activated() {
    if (this.$route.query.id && this.$route.query.id != this.id) {
      // 有id就修改
      this.getCaseInfo(this.$route.query.id); //获取表单数据
    }
  },
  methods: {
    /** 查询慢病中心-分类列表 */
    getCaseClassList() {
      this.loading = true;
      listCaseClass(this.queryParams).then((response) => {
        this.caseClassList = this.handleTree(response.data, "id", "parentId");
      });
    },
    /** 转换慢病中心-文章分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      // 关闭当前tabs
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.replace({
        path: "/chronicDisease/case",
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        excerpt: null,
        picture:null,
        content: null, 
        author: "南昌众康",
        caseClassId: null,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    getCaseInfo(id) {
      this.reset();
      getCase(id).then((response) => {
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCase(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              // 关闭当前tabs
              this.$store.dispatch("tagsView/delView", this.$route);
              this.$router.replace({
                path: "/chronicDisease/case",
              });
            });
          } else {
            addCase(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              // 关闭当前tabs
              this.$store.dispatch("tagsView/delView", this.$route);
              this.$router.replace({
                path: "/chronicDisease/case",
              });
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  justify-content: space-between;
}
.box-card {
  width: 48%;
}
.dialog-footer {
  text-align: right;
}
.preview {
  width: 100%;
  .preview-title {
    font-size: 22px;
    font-weight: bold;
    margin-bottom: 10px;
    text-align: center;
  }
  .preview-info {
    display: flex;
    align-items: center;
    margin-top: 30px;
    .info-text {
      font-size: 16px;
      margin-right: 100px;
      color: #666;
    }
  }
  .preview-excerpt {
    font-size: 16px;
    margin-top: 30px;
    line-height: 30px;
    background: #f5f5f5;
    padding: 10px 15px;
  }
}
</style>
<style>
.preview-content {
  margin-top: 30px;
  img {
    max-width: 100% !important;
  }
}
</style>
